home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP0492.ARJ / QBSORT.C < prev    next >
Text File  |  1991-09-07  |  2KB  |  62 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*      Quick single-pass bubble sort                             */
  4. /*      (OK, I know it's an oxymoron, but it's fast enough        */
  5. /*       for small arrays and it's _VERY_ small!)                 */
  6. /*                                                                */
  7. /******************************************************************/
  8.  
  9. enum LOGICAL {ERROR = -1, FALSE, TRUE};
  10.  
  11. qbsort(char *str_array[], int number)
  12. {
  13.       int backup = FALSE, i = 0, j = 1, last = 0;
  14.       void _pascal swapem(char *[], int, int);
  15.  
  16.       while ((i + 1) < number)
  17.       {
  18.             if (strcmp(str_array[i], str_array[j]) <= 0)
  19.             {
  20.                   /*  In order continue...                        */
  21.                   if (backup)
  22.                   {
  23.                         /*  If we were reversing, quit            */
  24.                         i = j = last;
  25.                         ++j;
  26.                         backup = FALSE;
  27.                   }
  28.                   else
  29.                   {     /*  Go on to next entries                 */
  30.                         ++i;
  31.                         ++j;
  32.                   }
  33.                   last = 0;   /*  Flag top of sort                */
  34.             }
  35.             else
  36.             {
  37.                   /*  Out of order - backup and swap              */
  38.                   backup = TRUE;
  39.                   if (!last) /*  Remember where we were           */
  40.                         last = j;
  41.                   if (i == 0)
  42.                   {
  43.                         /*  At bottom, swap and goto top*/
  44.                         swapem(str_array, i, j);
  45.                         i = j = last;
  46.                         ++j;
  47.                         backup = last = 0;
  48.                   }
  49.                   else  swapem(str_array, i--, j--);
  50.             }
  51.       }
  52. }
  53.  
  54. static void _pascal swapem(char *str_array[], int last, int now)
  55. {
  56.       register char *s_temp;
  57.  
  58.       s_temp            = str_array[last];
  59.       str_array[last] = str_array[now];
  60.       str_array[now]    = s_temp;
  61. }
  62.